package com.mikasa.rabbitmq.quickstart;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Envelope;
import com.rabbitmq.client.QueueingConsumer;
import com.rabbitmq.client.QueueingConsumer.Delivery;

/**
 * @author mikasa
 */
public class Consumer {

	public static void main(String[] args) throws Exception {
		// 1.获取连接工厂
		ConnectionFactory connectionFactory = new ConnectionFactory();
		// 配置连接信息
		connectionFactory.setHost("39.108.108.234");
		connectionFactory.setPort(5672);
		connectionFactory.setVirtualHost("/");

		// 2.创建连接
		Connection connection = connectionFactory.newConnection();

		// 3.创建信道 :所有的操作都是在信道中完成的！
		Channel channel = connection.createChannel();

		// 4.声明队列
		String queueName = "test001";
		// String queue, boolean durable, boolean exclusive, boolean autoDelete,Map<String, Object> arguments
		channel.queueDeclare(queueName,true, false, false, null);

		// 5.创建消费者
		QueueingConsumer consumer = new QueueingConsumer(channel);

		// 6.设置信道
		channel.basicConsume(queueName, true, consumer);
		while (true) {
			// 7.接收消息
			Delivery delivery = consumer.nextDelivery();
			String msg = new String(delivery.getBody());
			System.err.println("消费端: " + msg);
			Envelope envelope = delivery.getEnvelope();
		}

		}


}

